#ifndef USER_SUBMISSION_MANAGER_HPP
#define USER_SUBMISSION_MANAGER_HPP

#include <string>
#include <vector>
#include <mysql/mysql.h>

class UserSubmissionManager {
public:
    // 构造函数，初始化数据库连接信息
    UserSubmissionManager()
        : host_("127.0.0.1"), user_("oj_client"), passwd_("123456"), db_("oj"), port_(3306) {}

    // 保存用户的提交记录
    bool SaveSubmission(int user_id, const std::string& question_id,
                        const std::string& status, const std::string& result, const std::string& code) {
        MYSQL* conn = mysql_init(nullptr);
        if (mysql_real_connect(conn, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0)) {
            std::string query = "INSERT INTO user_submissions (user_id, question_id, status, result, code) "
                                "VALUES (" + std::to_string(user_id) + ", '" + question_id + "', '" +
                                status + "', '" + result + "', '" + code + "')";
            if (mysql_query(conn, query.c_str()) == 0) {
                mysql_close(conn);
                return true;
            } else {
                mysql_close(conn);
                return false;
            }
        }
        mysql_close(conn);
        return false;
    }

    // 获取某个用户对某题的最后一次提交代码
    bool GetLastSubmission(int user_id, const std::string& question_id, std::string* code) {
        MYSQL* conn = mysql_init(nullptr);
        if (mysql_real_connect(conn, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0)) {
            std::string query = "SELECT code FROM user_submissions WHERE user_id = " + std::to_string(user_id) +
                                " AND question_id = '" + question_id + "' ORDER BY submission_time DESC LIMIT 1";
            mysql_query(conn, query.c_str());
            MYSQL_RES* res = mysql_store_result(conn);
            if (mysql_num_rows(res) > 0) {
                MYSQL_ROW row = mysql_fetch_row(res);
                *code = row[0] ? row[0] : "";
                mysql_free_result(res);
                mysql_close(conn);
                return true;
            }
            mysql_free_result(res);
            mysql_close(conn);
        }
        mysql_close(conn);
        return false;
    }

    // 获取某个用户所有提交的记录
    bool GetUserSubmissions(int user_id, std::vector<std::string>& submissions) {
        MYSQL* conn = mysql_init(nullptr);
        if (mysql_real_connect(conn, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0)) {
            std::string query = "SELECT question_id, status, result, code FROM user_submissions WHERE user_id = " + std::to_string(user_id);
            mysql_query(conn, query.c_str());
            MYSQL_RES* res = mysql_store_result(conn);
            if (mysql_num_rows(res) > 0) {
                MYSQL_ROW row;
                while ((row = mysql_fetch_row(res)) != nullptr) {
                    std::string submission = "Question: " + std::string(row[0]) + ", Status: " + row[1] + ", Result: " + row[2];
                    submissions.push_back(submission);
                }
                mysql_free_result(res);
                mysql_close(conn);
                return true;
            }
            mysql_free_result(res);
            mysql_close(conn);
        }
        mysql_close(conn);
        return false;
    }

private:
    // 初始化数据库连接信息
    std::string host_ = "127.0.0.1";   // 数据库主机地址
    std::string user_ = "oj_client";   // 数据库用户名
    std::string passwd_ = "123456";    // 数据库密码
    std::string db_ = "oj";            // 数据库名称
    int port_ = 3306;                  // 数据库端口
};

#endif // USER_SUBMISSION_MANAGER_HPP
